package com.pic.mgt.action;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.pic.mgt.log.IgxLog;
import com.pic.mgt.manager.business.BusinessManagementOperationException;
import com.pic.mgt.manager.business.BusinessManager;
import com.pic.mgt.scheduler.Scheduler;
import com.pic.mgt.scheduler.SchedulerException;
import com.pic.mgt.scheduler.SchedulerImpl;


public class BusinessListSyncMgtActionImpl implements BusinessListSyncMgtAction {
	private BusinessManager businessManager;
	private Scheduler scheduler;
	private Class syncBizListExecutionQuartzJobClass;
	
	@Override
	public boolean addSyncBizListSchedule(String scheduleName,
			String cronExpression, String scheduleDescrption) {
		if (null == scheduleName || scheduleName.isEmpty() ||
			null == cronExpression || cronExpression.isEmpty()) {
			// ********************** Log **********************
			IgxLog.messageLog.warn(this,
							"Adding business list synchronization schedule failed. Did not provide schedule name or cron expression.");
			// ********************** Log **********************
			
			return false;
		}
		
		try {
			this.scheduler.scheduleCronJob(cronExpression, scheduleName,
										   BUSINESSLIST_SYNC_SCHEDULER_GROUPNAME,
										   scheduleDescrption, syncBizListExecutionQuartzJobClass,
										   Collections.emptyMap());
			
			return true;
		} catch (SchedulerException e) {
			// 不用寫 Log (已寫)
			
			return false;
		}
	}

	@Override
	public boolean modifySyncBizListSchedule(String scheduleName,
			String cronExpression, String scheduleDescrption) {
		if (null == scheduleName || scheduleName.isEmpty()) {
				// ********************** Log **********************
				IgxLog.messageLog.warn(this,
								"Modifying business list synchronization schedule failed. Did not provide schedule name or cron expression.");
				// ********************** Log **********************
				
				return false;
			}
			
			
			try {
				this.scheduler.rescheduleCronJob(cronExpression, scheduleName,
												BUSINESSLIST_SYNC_SCHEDULER_GROUPNAME,
												scheduleDescrption);
				
				return true;
			} catch (SchedulerException e) {
				// 不用寫 Log (已寫)
				
				return false;
			}
	}

	@Override
	public boolean removeSyncBizListSchedule(String scheduleName) {
		if (null == scheduleName || scheduleName.isEmpty()) {
			// ********************** Log **********************
			IgxLog.messageLog.warn(this,
							"Removing business list synchroniztion schedule failed. Did not provide schedule name.");
			// ********************** Log **********************
			
			return false;
		}
		
		
		try {
			this.scheduler.removeJob(scheduleName, BUSINESSLIST_SYNC_SCHEDULER_GROUPNAME);
			
			return true;
		} catch (SchedulerException e) {
			// 不用寫 Log (已寫)
			
			return false;
		}
	}

	@Override
	public boolean synchronizeBusinessList() {
		try {
			this.businessManager.synchronizeBusiness();
			
			return true;
		} catch (BusinessManagementOperationException e) {
			// 不用寫 log (已寫)
			return false;
		}
	}
	
	@Override
	public String getSyncBizListScheduleInfoByBizName() {
		try {
			// throws SchedulerException
			List<Map<String, Object>> jobInfoMapList =
				this.scheduler.getScheduleJobInfoByGroupName(BUSINESSLIST_SYNC_SCHEDULER_GROUPNAME);
			
			return this.scheduler.formatSchedules(jobInfoMapList);
		} catch (SchedulerException e) {
			// 不用寫 log
			return null;
		}
	}
	
	// --- getter and setter ---

	public void setBusinessManager(BusinessManager businessManager) {
		this.businessManager = businessManager;
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	public void setSyncBizListExecutionQuartzJobClass(
			Class syncBizListExecutionQuartzJobClass) {
		this.syncBizListExecutionQuartzJobClass = syncBizListExecutionQuartzJobClass;
	}

}
